package com.duapk.index.util;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.duapk.index.constant.Constant;

/**
 * 字符串操作工具类
 * 
 * @Package: com.duapk.index.util
 * @ClassName: StringTools
 * @author: jiangzheng
 * @date: Apr 5, 2011 4:53:37 PM
 * 
 */
public final class StringTools {

	private static Logger logger = LoggerFactory.getLogger(StringTools.class);

	/**
	 * 将字符串转化为float
	 * 
	 * @param str
	 * @param defaultNumber：当转换失败时给定的默认值
	 * @return
	 */
	public static float parseFloat(String str, float defaultNumber) {
		float number;
		try {
			number = Float.parseFloat(str);
		} catch (NumberFormatException e) {
			number = defaultNumber;
		}
		return number;
	}

	public static int parseInt(String str, int defaultNumber) {
		int number;
		try {
			number = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			number = defaultNumber;
		}
		return number;
	}

	public synchronized static long parseTime(String str, SimpleDateFormat sdf,
			long defaultNumber) {
		long number = 0;
		if (str != null && !str.isEmpty()) {
			try {
				number = sdf.parse(str).getTime();
			} catch (ParseException e) {
				number = defaultNumber;
			}

		}
		return number;
	}

	/**
	 * 获取绝对路径
	 * 
	 * @param subPath
	 * @return
	 */
	public static String getAbsolutePath(String subPath) {
		if (!subPath.startsWith("/")) {
			return Constant.APP_PATH + File.separator + subPath;
		}
		return subPath;
	}

	/**
	 * 去除字符串中的所有空白字符及标点符号
	 * 
	 * @param str
	 *            源字符串
	 * @return
	 */
	public static String trim(String str) {
		if (str == null || "".equals(str.trim())) {
			return "";
		}
		str = str.replaceAll("[\\s]|[\\p{Punct}]", "");
		return str;
	}

	/**
	 * 获取开始时间
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date getStartTime(String dateStr, String defaultTime) {
		DateFormat df = new SimpleDateFormat("HH:mm");
		Date date = null;
		Calendar date2 = new GregorianCalendar();
		try {
			date = df.parse(dateStr);
		} catch (Exception e) {
			logger.error("获取开始时间出错，", e);
			try {
				date = df.parse(defaultTime);
			} catch (Exception e1) {
				date = new Date();
			}
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		date2.set(GregorianCalendar.HOUR_OF_DAY, calendar
				.get(Calendar.HOUR_OF_DAY));
		date2.set(GregorianCalendar.MINUTE, calendar.get(Calendar.MINUTE));
		date = date2.getTime();
		// 如果启动时间在配置的时间之后，则将首次导入时间加1天。
		if (date.before(new GregorianCalendar().getTime())) {
			date2.add(Calendar.DATE, 1);
			date = date2.getTime();
		}
		return date;
	}

	/**
	 * 获取指定字符串的hashCode
	 * 
	 * @Title: getHashValue
	 * @author: jiangzheng
	 * @date: Jun 7, 2011 1:28:16 PM
	 * @param src
	 * @return
	 */
	public static int getHashValue(String src) {
		if (src == null || src.isEmpty()) {
			return 0;
		}
		return src.hashCode();
	}
}
